package application.visual;

import info.clearthought.layout.TableLayout;
import io.IOClient;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import application.Launcher;

public class DisconnectDialog extends JDialog{

	private JButton cancelButton, confirmButton;
	private JComboBox clientList;
	
	public DisconnectDialog()
	{
		super((JFrame)null, true);
		initGUI();
	}
	
	private JButton getCancelButton()
	{
		if(cancelButton == null){
			cancelButton = new JButton("Cancel");
			cancelButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					dispose();
				}
			});
		}
		return cancelButton;
	}
	
	private JButton getConfirmButton()
	{
		if(confirmButton == null)
		{
			confirmButton = new JButton("Disconnect");
			confirmButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					Launcher.getMainFrame().removeConnection((IOClient)getClientList().getSelectedItem());
					dispose();
				}
			});
		}
		
		return confirmButton;
	}
	
	private JComboBox getClientList()
	{
		if(clientList == null)
		{
			clientList = new JComboBox();
			for(IOClient cl : Launcher.getMainFrame().getConnectedClients())
				clientList.addItem(cl);
		}
		
		return clientList;
	}
	private void initGUI()
	{
		JPanel content = new JPanel();
		TableLayout layout = new TableLayout(new double[][]{
				{10, 100,100 , 10},{25, 25, 30}
		});
		
		content.setLayout(layout);
		
		content.add(getClientList(), "1,1,2,1,f,f");
		content.add(new JLabel("Select a client:"), "1,0,2,0,l,b");
		content.add(getCancelButton(), "1,2,1,2,l,c");
		content.add(getConfirmButton(), "2,2,2,2,r,c");
		
		getContentPane().add(content);
		pack();
	}
	
}
